<?php

namespace App\Admin\Controllers;

use App\Models\BannerModel;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class BannerController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '轮播图管理';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new BannerModel());
        $grid->model()->orderByDesc('id');

        $grid->column('id', 'ID');
        $grid->column('title', '标题');
        $grid->column('subtitle', '副标题');
        $grid->column('image', '轮播图')->lightbox(['width'=>100,'height'=>80]);
        $grid->column('location', '位置');
        $grid->column('type', '类型')->using(BannerModel::TYPE);
        $grid->column('status', '状态')->using(BannerModel::STATUS)->label(['warning','success']);
        $grid->column('created_at', '创建时间');

        $grid->disableFilter();
        $grid->disableExport();
        $grid->disableRowSelector();
        $grid->disableColumnSelector();

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(BannerModel::findOrFail($id));

        $show->field('id', 'ID');
        $show->field('title', '标题');
        $show->field('subtitle', '副标题');
        $show->field('image', '轮播图');
        $show->field('location', '位置');
        $show->field('type', '类型')->using(BannerModel::TYPE);
        $show->field('status', '状态')->using(BannerModel::STATUS);
        $show->field('created_at', '创建时间');

        $show->panel()
            ->tools(function ($tools) {
                $tools->disableEdit();
                $tools->disableDelete();
            });

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new BannerModel());

        $form->text('title', '标题')->required();
        $form->text('subtitle', '副标题');
        $form->image('image', '轮播图')->required();
        $form->number('location', '位置')->default(99);
        $form->select('type', '类型')->options(BannerModel::TYPE);
        $form->switch('status', '状态')->states(BannerModel::STATUS_VAL);

        $form->tools(function (Form\Tools $tools) {
            $tools->disableDelete();
            $tools->disableView();
        });
        $form->footer(function ($footer) {
            $footer->disableReset();
            $footer->disableViewCheck();
            $footer->disableEditingCheck();
            $footer->disableCreatingCheck();
        });

        return $form;
    }
}
